package com.app.params.utlis;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Sharedpreference {
    public static Sharedpreference shareinfo = null;
    private Editor ed;
    public static Context context;
    private static SharedPreferences SP;

    public static Sharedpreference getinitstance() {
        if (shareinfo == null)
            init();
        return shareinfo;
    }

    public static void setContext(Context context) {
        Sharedpreference.context = context;
    }

    private static synchronized void init() {
        if (context != null) {
            shareinfo = new Sharedpreference(context);
        }
    }

    public Sharedpreference(Context context) {
        if (null == SP) {
            SP = context.getSharedPreferences("Userinfos", 0);
        }
    }

    public void setint(String key, int value) {
        Editor ed = SP.edit();
        ed.putInt(key, value);
        ed.commit();
    }

    public int getint(String key) {
        return SP.getInt(key, 0);
    }

    public void setstring(String key, String value) {
        if (null == ed) {
            ed = SP.edit();
        }
        ed.putString(key, value);
        ed.commit();
    }

    public void remover(String key) {
        Editor ed = SP.edit();
        ed.remove(key);
        ed.commit();
    }

    public String getstring(String key) {
        return SP.getString(key, "");
    }

    public void setboolean(String key, boolean value) {
        Editor ed = SP.edit();
        ed.putBoolean(key, value);
        ed.commit();
    }

    public void clear() {
        Editor ed = SP.edit();
        if (ed != null) {
            ed.clear();
            ed.commit();
        }
    }

    public void put(String key, Object object) {
        if (null == ed) {
            ed = SP.edit();
        }
        if (object instanceof String) {
            ed.putString(key, (String) object);
        } else if (object instanceof Integer) {
            ed.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            ed.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            ed.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            ed.putLong(key, (Long) object);
        } else {
            ed.putString(key, object.toString());
        }
        ed.commit();
    }

    public boolean getBoolean_f(String key) {
        return SP.getBoolean(key, false);
    }

}
